package design.createModel.factory.configFactory;

import design.createModel.factory.abstractFactory.Coffee;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

public class CoffeeFactory {

    public static Map<String,Coffee> factory = new ConcurrentHashMap<>();

    static {
        Properties properties = new Properties();

        InputStream stream = CoffeeFactory.class.getClassLoader().getResourceAsStream("bean.properties");

        try (stream){
            properties.load(stream);
            for (Object key : properties.keySet()) {
                String className = properties.getProperty((String) key);

                Class<?> aClass = Class.forName(className);

                factory.put((String) key,(Coffee)aClass.newInstance());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Coffee createBean(String name){
        return factory.get(name);
    }
}
